using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class CameraStateTriggerVolumn : MonoBehaviour
{
    // 模式
    public enum TriggerMode
    {
        TARGET,
        DIFF
    }
    public TriggerMode mode = TriggerMode.TARGET;

    // 目标数值偏移
    public Vector3 offset;
    public Vector2 directions;
    public float rotation;
    public float distance;
    // 新的目标值
    public GameObject newTargetObject;
    public float L0Weight;
    public float L1Weight;
    public bool forceSync = false;

    // 标记是否在动画中
    public bool animating = false;

    // 暂存状态
    private CameraMovement states = null;
    private GameObject _targetObject;
    private Vector3 _targetOffset;
    private Vector2 _targetDirections;
    private float _targetRotation;
    private float _targetDistance;
    private float _L0Weight = 32;
    private float _L1Weight = 16;
    private bool _forceSync;
    private Animator animator = null;
    private bool insideVolumn = false;


    private void Start()
    {
        animator = GetComponent<Animator>();
        if (animator != null)
        {
            animator.SetBool("InsideVolumn", insideVolumn);
        }
    }


    private void OnTriggerExit(Collider other)
    {
        states = CameraManager.instance.GetActiveCamera().GetComponent<CameraMovement>();
        if (animator == null || !animator.enabled)
        {
            // restore values
            if (mode == TriggerMode.DIFF)
            {
                states.targetDirections -= directions;
                states.targetDistance -= distance;
                states.targetOffset -= offset;
                states.targetRotation -= rotation;
            }
            else if (mode == TriggerMode.TARGET)
            {
                states.targetDirections = _targetDirections;
                states.targetDistance = _targetDistance;
                states.targetOffset = _targetOffset;
                states.targetRotation = _targetRotation;
            }
        }
        states.targetObject = _targetObject;
        states.L0Weight = _L0Weight;
        states.L1Weight = _L1Weight;
        states.forceSync = _forceSync;

        insideVolumn = false;
    }

    private void OnTriggerEnter(Collider other)
    {
        states = CameraManager.instance.GetActiveCamera().GetComponent<CameraMovement>();
        // record old values
        _targetDirections = states.targetDirections;
        _targetDistance = states.targetDistance;
        _targetOffset = states.targetOffset;
        _targetRotation = states.targetRotation;
        _L0Weight = states.L0Weight;
        _L1Weight = states.L1Weight;
        _forceSync = states.forceSync;
        _targetObject = states.targetObject;
        // animate
        if (animator == null || !animator.enabled)
        {
            // set new values
            if (mode == TriggerMode.DIFF)
            {
                states.targetDirections += directions;
                states.targetDistance += distance;
                states.targetOffset += offset;
                states.targetRotation += rotation;
            }
            else if (mode == TriggerMode.TARGET)
            {
                states.targetDirections = directions;
                states.targetDistance = distance;
                states.targetOffset = offset;
                states.targetRotation = rotation;
            }
        }
        // the rest will happen in Update()
        states.targetObject = newTargetObject;
        states.L0Weight = L0Weight;
        states.L1Weight = L1Weight;
        states.forceSync = forceSync;

        insideVolumn = true;
    }

    private void Update()
    {
        states = CameraManager.instance.GetActiveCamera().GetComponent<CameraMovement>();
        if (states != null && animator != null && animator.enabled)
        {
            animator.SetBool("InsideVolumn", insideVolumn);

            if (animating)
            {
                if (mode == TriggerMode.DIFF)
                {
                    // set new values
                    states.targetDirections = directions + _targetDirections;
                    states.targetDistance = distance + _targetDistance;
                    states.targetOffset = offset + _targetOffset;
                    states.targetRotation = rotation + _targetRotation;
                }
                else if (mode == TriggerMode.TARGET)
                {
                    states.targetDirections = directions;
                    states.targetDistance = distance;
                    states.targetOffset = offset;
                    states.targetRotation = rotation;
                }
            }

            states.forceSync = forceSync;
        }
    }
}